/*
 * Bombertest.java
 *
 * Created on March 24, 2008, 2:28 PM
 */
package applet;

import bomberman.DB;
import bomberman.Game;
import bomberman.img.ImageDB;
import bomberman.img.ViewAll;
import java.awt.image.BufferedImage;
import server.Server;

/**
 *
 * @author  eirikb
 */
public class Bombertest extends javax.swing.JApplet {

    private Server server;
    private ImageDB idb;
    private Game game;
    private final int FPS = 50;
    private final int W = 656;
    private final int H = 496;

    /** Initializes the applet Bombertest */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        setSize(W, H);
        try {
            idb = DB.load().getIdb();
            idb.generateImages();
        } catch (Exception e) {
        }

        new Run().start();

    }

    private class Run extends Thread {

        @Override
        public void run() {
            try {
                int FPSDiff = FPS;
                BufferedImage bg = createBackground();
                while (true) {
                    gamePanel.getGraphics().drawImage(bg, 0, 0, null);

                    long time = System.currentTimeMillis();
                    Thread.sleep(FPSDiff);
                    time = System.currentTimeMillis() - time;
                    if (time > FPS) {
                        FPSDiff--;
                    } else {
                        FPSDiff++;
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    public BufferedImage createBackground() {
        BufferedImage bg = new BufferedImage(W, H, BufferedImage.TYPE_4BYTE_ABGR);
        for (int y = 16; y < H - 16; y += 32) {
            for (int x = 16; x < W - 16; x += 32) {
                bg.getGraphics().drawImage(idb.getBox().get("default").getImage(), x, y, null);
            }
        }
        return bg;
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gamePanel = new javax.swing.JPanel();

        getContentPane().setLayout(new java.awt.CardLayout());

        gamePanel.setBackground(java.awt.Color.orange);
        gamePanel.setDoubleBuffered(true);

        org.jdesktop.layout.GroupLayout gamePanelLayout = new org.jdesktop.layout.GroupLayout(gamePanel);
        gamePanel.setLayout(gamePanelLayout);
        gamePanelLayout.setHorizontalGroup(
            gamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 458, Short.MAX_VALUE)
        );
        gamePanelLayout.setVerticalGroup(
            gamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 369, Short.MAX_VALUE)
        );

        getContentPane().add(gamePanel, "card2");
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel gamePanel;
    // End of variables declaration//GEN-END:variables
}
